#include "HaLL_sensor.h"
#include "stm32f4xx.h"                  // Device header
static uint16_t sg_Hall_Cnt=0;

Sector_info g_SectorTable[6]={
   {.UVW={0,0,1},.Sector=1},
   {.UVW={1,0,1},.Sector=2},
   {.UVW={1,0,0},.Sector=3},
   {.UVW={1,1,0},.Sector=4},
   {.UVW={0,1,0},.Sector=5},
   {.UVW={0,1,1},.Sector=6}
};
static uint8_t g_HalInputChanged_Flag=0;

void Set_Hall_Global_IrFlag(uint8_t val)
{
    g_HalInputChanged_Flag = val;
}

uint8_t Get_Hall_Global_IrFlag(void)
{
    return g_HalInputChanged_Flag;
}

uint8_t Get_HALLMotorSector_Loc(void)
{
    uint8_t i,Retval=0xFF;

    Sector_info Current_HallOutput;
    memset(&Current_HallOutput,0x00,sizeof(Sector_info));

    uint8_t U,V,W;
    U=HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_U);
    V=HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_V);
    W=HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_W);

    Current_HallOutput.UVW[0]=U;
    Current_HallOutput.UVW[1]=V;
    Current_HallOutput.UVW[2]=W;

    for(i=0;i<6;i++)
    {
        if( 0 == memcmp(Current_HallOutput.UVW,g_SectorTable[i].UVW,3) )
        {
            Retval=g_SectorTable[i].Sector;
            break;
        }
    }

    return Retval;

}

void Usart1_Print_Hallinput(void)
{
    printf("U=%d ",HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_U));
    printf("V=%d ",HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_V));
    printf("W=%d\n",HAL_GPIO_ReadPin(Hall_Sensor_Port,Hall_Sensor_W));
}

void v_SetHallCnt(uint16_t val)
{
    sg_Hall_Cnt=val;
}

uint16_t u16_GetHallCnt(void)
{
    return sg_Hall_Cnt;
}